﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Bussiness;
using HTTT.DataAccess;
using HTTT.AD;
using System.Configuration;

namespace HTTT.admin.controls
{
    public partial class ucAddGroupRequestDetail : System.Web.UI.UserControl
    {
        private Guid _requestID;
        public Guid RequestID
        {
            get { return _requestID; }
            set { _requestID = value; /*LoadData();*/ }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            LoadRequestDetail();
            string ouPathNewGroup = LoadAddGroupDetail();
            LoadOU(ddlOU, ouPathNewGroup);
            SetReadOnly(true);
        }

        private void LoadRequestDetail()
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(RequestID);
            ltlActionName.Text = MyRequest.ActionName;
            txtFromRequest.Text = MyRequest.Note;
            txtRequestedBy.Text = MyRequest.Requestedby;
            txtRequestedDate.Text = MyRequest.RequestDate.ToString();
        }

        private void SetReadOnly(bool IsReadOnly)
        {
            // Get all TextBoxes and set the value of the ReadOnly property.
            foreach (var tb in this.Controls.OfType<TextBox>())
                tb.ReadOnly = IsReadOnly;
            ddlOU.Enabled = !IsReadOnly;
            rblGroupScope.Enabled = !IsReadOnly;
            rblGroupType.Enabled = !IsReadOnly;
        }

        
        private void LoadOU(DropDownList ddl, string selectedValue)
        {
            //Get OU list
            HT_OUBO Bo = new HT_OUBO();
            List<HT_OU> ListBO = Bo.GetAllHT_OU().ToList();
            ddl.DataSource = ListBO;
            ddl.DataValueField = ADOUProperties.DISTINGUISHEDNAME;
            ddl.DataTextField = ADOUProperties.NAME;
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("▼ " + ConfigurationManager.AppSettings["LDAPDomain"], ConfigurationManager.AppSettings["DomainPath"]));
            ddl.SelectedValue = selectedValue;
        }
        private string LoadAddGroupDetail()
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(RequestID);

            HT_GroupsBO GroupBO = new HT_GroupsBO();
            HT_Groups Info = GroupBO.GetHTGroupFromAddGroupData(MyRequest.ActionDescription);

            txtName.Text = Info.name;
            txtDescription.Text = Info.description;
            rblGroupScope.SelectedValue = Info.groupScope.ToString();
            rblGroupType.SelectedValue = Convert.ToInt16(Info.isSecurityGroup).ToString();
            //txtMember.Text = Info.members;
            string ouPath = Info.LDAPPath;
            ouPath = ouPath.Substring(ouPath.IndexOf(',') + 1);
            return ouPath;
        }

    }
}